home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / Extensions… / Extension Shell ƒ / Extension.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  7.7 KB  |  286 lines  |  [TEXT/MPS ]

  1. /*________________________________________________________
  2.  
  3.     File: Extension.c
  4.  
  5.     C code for a printing extension.
  6.  
  7.     Dave Hersey
  8.     Apple Developer Technical Support
  9.  
  10.     12/01/92 - dmh - Created.
  11.      4/26/93 - dmh - Updated to use recommended approach
  12.                       to global data initialization.
  13.      9/05/93 - dmh - Updated for b2.
  14.                     - Fixed minor problem with highlighting
  15.                      of editText panel items.
  16.                    - Switched to Exception.h assertion stuff
  17.                      for error checking.
  18.     12/18/93 - dmh - Updated for b3.
  19.      3/22/94 - dmh - Updated for b4.
  20.  
  21.     (Note: all functions are in the Mark menu.)
  22.     
  23. __________________________________________________________*/
  24.  
  25. #include "Extension.h"
  26.  
  27.  
  28. /*******************************************************************
  29.     InitGlobalData is used to initialize any global data we need to
  30.     in our initialize message override.  It's critical that you do
  31.     things this way, rather than access the data in the same scope
  32.     that you call NewMessageGlobals.  Otherwise, some compilers
  33.     will give you code that references an invalid A5 world.
  34.  
  35. ********************************************************************/
  36.  
  37. OSErr InitGlobalData()
  38. {
  39. // Initialize any global data here.
  40.     
  41.     return noErr;
  42. }
  43.  
  44.  
  45. /*******************************************************************
  46.     NewInitialize is our override for the GXInitialize message.  In
  47.     here, you shouldn't initialize anything directly-- call
  48.     InitGlobalData for that.  Once you create the A5 world with
  49.     NewMessageGlobals, you can access your global data just like
  50.     you were an application.  Whenever you're called, your global
  51.     data will be valid.
  52.     
  53. ********************************************************************/
  54.  
  55. OSErr NewInitialize()
  56. {
  57.     OSErr    err;
  58.  
  59. // Create an A5 world, and initialize our global data.
  60.  
  61.     err = NewMessageGlobals(A5Size(), A5Init);
  62.     
  63.     if (!err) err = InitGlobalData();
  64.     
  65.     return err;
  66. }
  67.  
  68.  
  69. /*******************************************************************
  70.     NewShutDown is our override for the GXShutDown message.  We
  71.     simply throw away our A5 world which we created in our
  72.     GXInitialize message override, NewInitialize.
  73.     
  74. ********************************************************************/
  75.  
  76. OSErr NewShutDown()
  77. {
  78.     DisposeMessageGlobals();
  79.     return noErr;
  80. }
  81.  
  82.  
  83. /*******************************************************************
  84.     NewSpoolPage is our override for the GXSpoolPage message.  We
  85.     check to see if we're enabled, and if so, give a SysBeep before
  86.     forwarding.
  87.     
  88. ********************************************************************/
  89.  
  90. OSErr NewSpoolPage(gxSpoolFile spFile, gxFormat aFormat, gxShape pgShape)
  91. {
  92.     OSErr                    err;
  93.     ExtensionCollection        extCollect;
  94.     
  95. // Try to retrieve our collection item.  If we can't find it, we'll
  96. // act as if the user had us turned off or on (as determined by
  97. // kDefaultSetting).
  98.  
  99.     err = GetJobCollectionItem(&extCollect, nil, kExtensionCollectionType,
  100.                                gxPrintingTagID);
  101.  
  102.     if (err)
  103.     {
  104.         extCollect.extTurnedOn = kDefaultSetting;
  105.         err = noErr;
  106.     }
  107.  
  108.  
  109. // If we're turned on, beep so we know we've been here, then forward
  110. // this message down the chain.
  111.  
  112.     if (extCollect.extTurnedOn)
  113.         SysBeep(3);
  114.     
  115.     err = Forward_GXSpoolPage(spFile, aFormat, pgShape);
  116.  
  117.     return err;
  118. }
  119.  
  120.  
  121. /*******************************************************************
  122.     NewJobPrintDialog is our override for GXJobPrintDialog.  All we
  123.     do is set up our panel and then forward the message.
  124.     
  125. ********************************************************************/
  126.  
  127. OSErr NewJobPrintDialog(gxDialogResult *dlogResult)
  128. {
  129.     OSErr    err;
  130.     
  131.     err = SetUpPrintPanel();
  132.  
  133.     if (!err)
  134.         err = Forward_GXJobPrintDialog(dlogResult);
  135.     
  136.     return err;
  137. }
  138.  
  139.  
  140. /*******************************************************************
  141.     NewHandlePanelEvent is our override for GXHandlePanelEvent. If
  142.     the event is one of ours, we handle it.  Otherwise, we just
  143.     forward it down the chain.
  144.     
  145. ********************************************************************/
  146.  
  147. OSErr NewHandlePanelEvent(gxPanelInfoRecord *panelInfo)
  148. {
  149.     OSErr            err = noErr;
  150.     GrafPtr            oldPort;
  151.     DialogPtr        pDlg;
  152.  
  153. // Get a pointer to the dialog, save our current grafPort,
  154. // and set us to the dialog's port.
  155.  
  156.     pDlg = panelInfo->pDlg;
  157.     GetPort(&oldPort);
  158.     SetPort(pDlg);
  159.  
  160.     switch (panelInfo->panelEvt)    // Handle any of these events as need be.
  161.     {
  162.         case gxPanelOpenEvt:                // Initialize and draw.
  163.             break;
  164.         case gxPanelCloseEvt:                // Your panel is going away (panel switch,
  165.             break;                            // confirm or cancel).
  166.  
  167.         case gxPanelHitEvt:                    // There's a hit in your panel.
  168.             break;
  169.         case gxPanelFilterEvt:                // This is called to filter every event.
  170.             break;
  171.         case gxPanelCancelEvt:                // The user has cancelled the dialog.
  172.             break;
  173.         case gxPanelConfirmEvt:                // The user has confirmed the dialog.
  174.             break;
  175.         case gxPanelUserWillConfirmEvt:        // user has selected confirm, time to
  176.             break;                            // parse your panel interdependencies.
  177.                                         
  178.         case gxPanelDialogEvt:                // Event to be handled by dialoghandler
  179.             break;                            // (you get to see all events).
  180.  
  181.         case gxPanelOtherEvt:                // osEvts, etc.
  182.             break;
  183.  
  184. // If our panel is activating/deactivating or if the focus (which
  185. // section of the dialog is active) has changed we need to activate
  186. // our editText fields appropriately.
  187.  
  188.         case gxPanelActivateEvt:            // The dialog window has just become active.
  189.         case gxPanelDeactivateEvt:            // The dialog window is becoming inactive.
  190.         case gxPanelIconFocusEvt:            // The user is moving to the icon list.
  191.         case gxPanelPanelFocusEvt:            // The user is moving to the panel.
  192.  
  193. /*  Here's what you would do if you had an editText field at DITL item #5:
  194.  
  195. #define d_edText    5
  196.  
  197.             if ((((DialogPeek) pDlg)->editField +1) == (panelInfo->itemCount +d_edText))
  198.             {
  199.                 if (panelInfo->panelEvt == gxPanelPanelFocusEvt)
  200.                     TEActivate(((DialogPeek) pDlg)->textH);
  201.                 else
  202.                     TEDeactivate(((DialogPeek) pDlg)->textH);
  203.             }
  204. */
  205.             break;
  206.     }
  207.  
  208. // Restore the original port as we leave.
  209.  
  210.     SetPort(oldPort);
  211.     return err;
  212. }
  213.  
  214.  
  215. /*******************************************************************
  216.     SetUpPrintPanel sets up our print panel, adding a default
  217.     ExtensionCollection item to the job collection.  This
  218.     collection item has the values we'll use to set up our panel's
  219.     controls.
  220.     
  221. ********************************************************************/
  222.  
  223. OSErr SetUpPrintPanel()
  224. {
  225.     OSErr                    err;
  226.     gxPanelSetupRecord        panelSetupRec;
  227.     ExtensionCollection        extConfig;
  228.  
  229. // Try to find our collection item.
  230.  
  231.     err = GetCollectionItem(GXGetJobCollection(GXGetJob()),
  232.                             kExtensionCollectionType,
  233.                             gxPrintingTagID,
  234.                             nil,
  235.                             &extConfig);
  236.  
  237.  
  238. // If we don't have an item in the job collection yet, store our default
  239. // settings in it.
  240.  
  241.     if (err == collectionItemNotFoundErr)
  242.     {
  243.         extConfig.extTurnedOn = kDefaultSetting;
  244.     
  245.         err = AddCollectionItem(GXGetJobCollection(GXGetJob()),
  246.                                 kExtensionCollectionType,
  247.                                 gxPrintingTagID,
  248.                                 sizeof(ExtensionCollection),
  249.                                 &extConfig);
  250.  
  251.         nrequire(err, HaveCollectionMgrError);
  252.     }
  253.  
  254.  
  255. // Now, set up the panel.
  256.  
  257.     panelSetupRec.panelResId        = r_ExtensionPanel;        // which panel resource?
  258.     panelSetupRec.resourceRefNum    = GXGetMessageHandlerResFile(); // where is it?
  259.     panelSetupRec.refCon            = 0;                       // we don't use this.
  260.     panelSetupRec.panelKind            = gxExtensionPanel;     // This is an extension panel.
  261.  
  262.     err = GXSetupDialogPanel(&panelSetupRec);
  263.  
  264.  
  265. HaveCollectionMgrError:
  266.     
  267.     return err;
  268. }
  269.  
  270.  
  271. /*******************************************************************
  272.     GetJobCollectionItem is a generic routine that retrieves a
  273.     collection item from the job collection.
  274.     
  275. ********************************************************************/
  276.  
  277. OSErr GetJobCollectionItem(void *collectItem, long *collectSize,
  278.                            OSType collectType, short collectID)
  279. {
  280.     return GetCollectionItem(GXGetJobCollection(GXGetJob()),
  281.                              collectType,
  282.                              collectID,
  283.                              collectSize,
  284.                              collectItem);
  285. }
  286.